//编辑限时统一折扣活动

//js去掉空格
var oldProducts="";
var oldProductNum="";
var oldsaleProducIds="";
function trim(str) {
	return (str + "").replace(/^\s+|\s+$/g, "");
}
function cancle() {
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}
function toAddProduct() {
	oldProducts=$('#productTbody').html();
	oldProductNum=$('#product_id').html();
	oldsaleProducIds=document.getElementById("saleProducIds").value;
	
	var sale = $("input[name='sale']").val();
	sale = trim(sale);
	if (sale <= 0 || sale > 1 || sale == 1) {
		layer.alert("折扣必须在0~1之间");
		return false;
	}
	var url = path + "/activity/to_add_product";
	var index = layer.open({
		type : 2,
		title : "添加到活动商品列表",
		scrollbar : false,
		content : url,
		area : [ '1000px', '600px' ],
		cancel: function(index, layero){ 
			
			$('#productTbody').html(oldProducts);
			$('#product_id').html(oldProductNum);
			$('#saleProducIds').val(oldsaleProducIds);
			
			} 
	});
	layer.full(index);
}
// 添加选中的商品ID
function addProduct(productId) {
	var productIds = document.getElementById("saleProducIds").value;
	var productIdArr = productIds.split(",");
	var exist = false;
	for (var i = 0; i < productIdArr.length; i++) {
		if (productIdArr[i] == productId) {
			exist = true;
			break;
		}
	}
	if (!exist) {
		if (productIds.length == 0) {
			productIds = productIds + productId + ",";
		} else {
			productIds = productIds + productId + ",";
		}
	}
	document.getElementById("saleProducIds").value = productIds;

	$("#saleProductLength").val(productIdArr.length);
}

// 删除商品ID
function deleteProduct(productId) {
	var productIds = document.getElementById("saleProducIds").value;
	var productIdArr = productIds.split(",");
	var result = "";
	for (var i = 0; i < productIdArr.length; i++) {
		if (productIdArr[i] != productId) {
			if (productIdArr.length - 1 == i) {
				result = result + productIdArr[i];
			} else {
				result = result + productIdArr[i] + ",";
			}
		}
	}
	document.getElementById("saleProducIds").value = result;

	$("#sale" + productId).remove();
	var saleLength = $("#saleProductLength").val();
	saleLength--;

	$("#saleProductLength").val(saleLength);
	$("#product_id").html("您已选择" + saleLength + "款商品");
}
$(function() {
	$("body").on("click", ".del-c", function() {
		var productId = $(this).attr("id");
		deleteProduct(productId);
	});
});
function sub_form() {
	var saleName = $("input[name='name']").val();
	saleName = trim(saleName);
	if (saleName == null || saleName == "") {
		layer.alert("请填写活动名称");
		return false;
	}
	var startDate = $("input[name='startDate']").val();
	startDate = trim(startDate);
	if (startDate == null || startDate == "") {
		layer.alert("请填写开始时间");
		return false;
	}
	var endDate = $("input[name='endDate']").val();
	endDate = trim(endDate);
	if (endDate == null || endDate == "") {
		layer.alert("请填写结束时间");
		return false;
	}
	var sale = $("input[name='sale']").val();
	sale = trim(sale);
	if (sale <= 0 || sale > 1 || sale == 1) {
		layer.alert("折扣必须在0~1之间");
		return false;
	}
	if (startDate > endDate || startDate == endDate) {
		layer.alert("结束时间必须大于开始时间");
		return false;
	}
	var length = $("#saleProductLength").val();
	var ids = document.getElementById("saleProducIds").value;
	if (trim(ids) == "" || trim(ids) == "," || length <= 0) {
		layer.alert("请选择添加的商品");
		return false;
	}
	var money = "";
	var mdata = "";
	var iserror = false;
	var activityType = $("#activityType option:selected").val();
	if (activityType == 2) {
		var i = 0;
		$('#productTable tr').each(function() {
			if (i == 0) {
				i++;
				return true;
			}
			var key = $(this).find("td:eq(0)").text();
			money = money + key + ":";
			var value = $(this).find(".money").val();
			value = trim(value);
			console.log(value);
			if (value == '' || value <= 0 || isNaN(value)) {
				layer.alert("价格输入有误");
				iserror = true;
				return false;
			}
			money = money + value + ";";
		});
		if (iserror) {
			return false;
		}
		mdata = $("#save_form").serialize() + "&remark=" + money;
	} else {
		var dec = $.trim($("#dec").val());
		if (dec == "") {
			layer.alert("请填写活动简介");
			return false;
		}
		mdata = $("#save_form").serialize();
	}
	layer.closeAll('dialog');
	var layer_load_window = layer.load(2);
	$.ajax({
		url : $("#save_form").attr("action"),
		cache : false,
		type : 'post',
		dataType : 'text',
		data : mdata,
		success : function(data) {
			var resultArr = data.split(",");
			if (resultArr[0] == "success") {
				layer.msg(resultArr[1], {
					icon : 1,
					time : 1000
				}, function() {
					var index = parent.layer.getFrameIndex(window.name);
					parent.location.reload();
					parent.layer.close(index);
				});
			} else if (resultArr[0] == "fail") {
				layer.alert(resultArr[1]);
			} else {
				layer.alert("操作失败!");
			}
		},
		error : function(data) {
			layer.alert("操作失败!");
		},
		complete : function(XMLHttpRequest, textStatus) {
			layer.close(layer_load_window);
		}
	})
}
function change() {
	var check = $("#activityType option:selected").val();
	if (check == 2) {
		$(".tehui").show();
		$(".xianshi").hide();
	} else if (check == 1) {
		$(".tehui").hide();
		$(".xianshi").show();
	}
	$('#dec').val("");
	$('#product_id').text("您已选择0款商品")
	$('#productTbody').empty();
	$('#saleProducIds').val("");
	$('#saleProductLength').val("0");
}